<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>new</title>
</head>
<body>
    <script>
        function Person(opt){
            const {name, age} = opt;
            this.name = name;
            this.age = age;
        }
        Person.prototype.sayName = function(){
            console.log(this.name);
        }
        Person.prototype.sayAge = function () {
            console.log(this.age);
        }

        var p1 = new Person({
            name: 'wjh',
            age: 19
        });
        p1.sayName();
        var p2 = myNew(Person, {
            name: 'lll',
            age: 10
        });
        p2.sayName();


        function myNew(fn){
            var obj = {};
            // 拷贝原型链上的方法
            obj.__proto__ = fn.prototype;
            var args = Array.prototype.slice.call(arguments, 1);
            // 执行构造函数获得对应的属性
            let res = fn.call(obj, ...args);
            return typeof res === 'object' ? result : obj;
        }
    </script>
</body>
</html>